import numpy as np
raw_df = np.loadtxt('logi-y.txt', delimiter=',', encoding='utf-8')
data = raw_df[:, 0:2]
target = raw_df[:, 2]
x = data
y = target
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
from sklearn.linear_model import LogisticRegression
log_model = LogisticRegression()
log_model.fit(x_train, y_train)
from sklearn.metrics import accuracy_score
y_pred = log_model.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("逻辑回归模型的准确率：", accuracy)
import matplotlib.pyplot as plt
x1_min, x1_max = x[:, 0].min() - 1, x[:, 0].max() + 1
x2_min, x2_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max, 500), np.linspace(x2_min, x2_max, 500))
Z = log_model.predict(np.c_[xx1.ravel(), xx2.ravel()])
Z = Z.reshape(xx1.shape)
plt.pcolormesh(xx1, xx2, Z, cmap=plt.cm.colors.ListedColormap(['#ACF080', '#A0A0FF']))
plt.scatter(x[y == 0, 0], x[y == 0, 1], c='blue', marker='o', label='未录取')
plt.scatter(x[y == 1, 0], x[y == 1, 1], c='red', marker='^', label='已录取')
plt.xlabel('科目1成绩', fontproperties='SimHei')plt.ylabel('科目2成绩', fontproperties='SimHei')
plt.legend(prop={'family': 'SimHei'})
plt.show()